// pages/orderInfo/orderInfo.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '订单详情',
    })
    console.log(options)
    this.setData({
      id: options.id
    })
    this.getOrderInfo();
  },

  getOrderInfo: function () {
    var that = this;
    this.data.pages++;
    var data = {
      point_url: app.server.agentApi.memberOrderDetail,
      openid: app.globalData.openid,
      order_id: this.data.id
    };
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    app.server.getConnect(data).then(function (res) {
      console.log(res);
      that.setData({
        orderInfo: res.data
      })
      wx.hideLoading();
    }, function (res) {
      console.log(res);
      wx.hideLoading();
      app.showConfirm('提示', res.msg, false);
    })
  },


  choicePayWay: function () {
    var that = this;
    app.showConfirm('付款方式', '', true, function () {
      that.setData({
        pay_type: 0
      })
      that.toPay();
    }, function () {
      that.setData({
        pay_type: 1
      })
      that.toPay();
    }, '余额', '赠送金')
  },

  toPay: function () {
    var that = this;
    var data = {
      point_url: app.server.agentApi.orderAction,
      openid: app.globalData.openid,
      id: this.data.id,
      pay_type: this.data.pay_type,
      action: 'pay',
      remarks: ''
    };
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    app.server.getConnect(data).then(function (res) {
      console.log(res);
      wx.showToast({
        title: '支付成功',
      })
      wx.hideLoading();
    }, function (res) {
      console.log(res);
      wx.hideLoading();
      app.showConfirm('提示', res.msg, false);
    })
  },

  toCancel: function (e) {
    var that = this;
    app.showConfirm('提醒', '是否确定删除订单', true, function () {
      var data = {
        point_url: app.server.agentApi.orderAction,
        openid: app.globalData.openid,
        id: that.data.id,
        pay_type: that.data.pay_type,
        action: 'cancel'
      };
      wx.showLoading({
        title: '加载中...',
        mask: true
      })
      app.server.getConnect(data).then(function (res) {
        console.log(res);
        wx.navigateBack({})
        wx.hideLoading();
      }, function (res) {
        console.log(res);
        wx.hideLoading();
        app.showConfirm('提示', res.msg, false);
      })
    })
  },

  copy:function(){
    wx.setClipboardData({
      data: this.data.orderInfo.order_sn,
      success(res) {
        wx.showToast({
          title: '已复制',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
